<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Abase_Asettings extends Controller_Abase_Acommon {

    public $template = 'alayout/amain';
    public $obj = NULL;

    public function showSettings() {
        if (!in_array(Cookie::get('role', null), unserialize($this->settings[$this->request->action()]['roles']))) die('Access denied!');

        if ($this->request->action() == 'account') {
            $settings = Model::factory('Aadmins')->currentUser();
            $view = 'account';
        } else {
            $settings = unserialize($this->settings[$this->request->action()]['_value']);
            $view = $this->settings[$this->request->action()]['view'];
        }

        $this->template->right = View::factory('asettings/'.$view, array(
            'title' => $this->settings[$this->request->action()]['title'],
            '_key' => $this->settings[$this->request->action()]['_key'],
            'settings' => $settings
        ));
    }

    private function saveSettings() {
        if (!in_array(Cookie::get('role', null), unserialize($this->settings[$this->request->action()]['roles']))) die('Access denied!');

        $obj = ORM::factory($this->request->controller())->where('_key', '=', $this->request->action())->find();
        $arr = unserialize($obj->_value);
        $password = $this->request->post('password');

        foreach ($arr as $key => $item) {
            if ($arr[$key]['type'] == 'password' && !empty($password)) {
                $arr[$key]['value'] = $this->request->post($item['key']);
            } elseif ($arr[$key]['type'] != 'password') {
                $arr[$key]['value'] = $this->request->post($item['key']);
            }

            if ($arr[$key]['type'] == 'image') {
                $arr[$key]['value'] = $this->request->post($item['key']);
                !empty($_FILES['source']['name']) ? $this->settingsImage($this->request->post('name'), $this->request->post('width'), $this->request->post('height')) : false;
            }
        }

        $obj->_value = serialize($arr);
        $obj->save();
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->action());
    }

    private function saveAccount() {
        $password = $this->request->post('password');
        $obj = ORM::factory('Aadmin')->where('id', '=', Cookie::get('admin', null))->find();
        $obj->login = $this->request->post('login');
        !empty($password) ? $obj->pass = md5($this->request->post('password')) : FALSE;
        $obj->fio = $this->request->post('fio');
        $obj->save();
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->action());
    }

    private function saveCss() {
        $obj = ORM::factory($this->request->controller())->where('_key', '=', $this->request->action())->find();
        $arr = unserialize($obj->_value);
        $arr['usercss']['value'] = $this->request->post('usercss');
        $obj->_value = serialize($arr);
        $obj->save();
        $this->saveToFile($_SERVER['DOCUMENT_ROOT'].'/static/uploads/settings/usercss.css', $arr['usercss']['value']);
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->action());
    }

    private function saveUserscripts() {
        $obj = ORM::factory($this->request->controller())->where('_key', '=', $this->request->action())->find();
        $arr = unserialize($obj->_value);
        $arr['userscripts']['value'] = $this->request->post('userscripts');
        $obj->_value = serialize($arr);
        $obj->save();
        $this->saveToFile($_SERVER['DOCUMENT_ROOT'].'/static/uploads/settings/userscripts.js', $arr['userscripts']['value']);
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->action());
    }

    private function settingsImage($name, $w, $h) {
        $path = $_SERVER['DOCUMENT_ROOT'].'/static/uploads/settings';
        $ext = pathinfo($_FILES['source']['name'], PATHINFO_EXTENSION);
        $name = $name/*.'.'.$ext*/;

        if (!is_dir($path)) {
            mkdir($path, 0755);
        }

        $name = Upload::save($_FILES['source'], $name, $path);

        Image::factory($name)
            ->resize($w, $h, Image::AUTO)
            ->save($name, 100);
    }

    public function action_account() {
        $this->showSettings();

        if ($this->request->param('save')) {
            $this->saveAccount();
        }
    }

    public function action_mailer() {
        $this->showSettings();

        if ($this->request->param('save')) {
            $this->saveSettings();
        }
    }

    public function action_yandexmap() {
        $this->showSettings();

        if ($this->request->param('save')) {
            $this->saveSettings();
        }
    }

    public function action_logo() {
        $this->showSettings();

        if ($this->request->param('save')) {
            $this->saveSettings();
        }
    }

    public function action_usercss() {
        $this->showSettings();

        if ($this->request->param('save')) {
            $this->saveCss();
        }
    }

    public function action_userscripts() {
        $this->showSettings();

        if ($this->request->param('save')) {
            $this->saveUserscripts();
        }
    }

}